﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Collections.Generic;
using Maticsoft.Model;
//using Maticsoft.BLL;
using Maticsoft.DAL;

namespace BuildingForm
{
    public partial class AllPersons : Form
    {
        Maticsoft.DAL.Ry_Info info = new Maticsoft.DAL.Ry_Info();
        string scode = "";
        string name = "";
        string sex = "";
        BlackPersonsForm f;
        public AllPersons(BlackPersonsForm fr)
        {
            InitializeComponent();
            Init();
            bind();
            if (fr != null)
            {
                f = fr;
            }
        }
        private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;
        private void Init()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            //表头加粗，居中
            foreach (DataGridViewColumn col in this.GridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.GridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.GridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.GridView1.AllowUserToAddRows = false;
            this.txtName.Text = "";
            this.txtSFZH.Text = "";
            this.panel1.BorderStyle = BorderStyle.FixedSingle;
            this.panel1.Hide();
            this.AutoScroll = true;
            panel1.Location = new Point((this.Size.Width - panel1.Size.Width) / 2, (this.Size.Height - panel1.Size.Height) / 2);
            hide();
        }
        private void show()
        {
            if (this.m_OpaqueLayer == null)
            {
                this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(125, false);
                this.Controls.Add(this.m_OpaqueLayer);
                this.m_OpaqueLayer.Dock = DockStyle.Fill;
                this.m_OpaqueLayer.BringToFront();
            }
            this.m_OpaqueLayer.Enabled = true;
            this.m_OpaqueLayer.Visible = true;

            //panel1在悬浮层上显示

            panel1.BackColor = Color.White;

            m_OpaqueLayer.Controls.Add(panel1);
            panel1.Show();
        }
        private void hide()
        {
            panel1.Hide();
            if (this.m_OpaqueLayer != null)
            {
                this.m_OpaqueLayer.Visible = false;
                this.m_OpaqueLayer.Enabled = false;
            }
        }
        private void bind()
        {
            string name = this.txtName.Text.ToString().Trim();
            string sfzh = this.txtSFZH.Text.ToString().Trim();
            string sql = "1=1";
            if (!string.IsNullOrEmpty(name))
            {
                sql += " and xm like '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(sfzh))
            {
                sql += " and sfzh like '%" + sfzh + "%'";
            }
            sql += " order by intime desc";
            DataTable dt = info.GetList(sql).Tables[0];
            this.GridView1.AutoGenerateColumns = false;
            this.GridView1.DataSource = dt;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bind();
        }

       

        private void GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;
            if(this.GridView1.Columns[e.ColumnIndex].Name == "colAdd")
            {
                show();
                DataGridViewRow r = GridView1.Rows[e.RowIndex];
                scode = r.Cells[2].Value.ToString();
                name = r.Cells[1].Value.ToString();
                sex = r.Cells[3].Value.ToString();
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Maticsoft.DAL.BlackPersons dao = new Maticsoft.DAL.BlackPersons();
            Maticsoft.Model.BlackPersons per = new Maticsoft.Model.BlackPersons();
            string remark = this.txtRemark.Text.ToString().Trim();
            if (string.IsNullOrEmpty(remark))
            {
                MessageBox.Show("请输入问题原因！"); return;
            }
            string code = scode;
            string sql = " SFZH='" + code + "'";
            DataTable dt = dao.GetList(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("该人员已存在！"); return;
            }
            per.Name = name;
            per.sex = sex == "男" ? true : false;
            per.SFZH = code;
            per.Remark = remark;
            per.createtime = DateTime.Now;
            bool isT = dao.Add(per) > 0;
            if (isT)
            {
                MessageBox.Show("添加黑名单成功！");
            }
            else
            {
                MessageBox.Show("添加黑名单失败！");
            }
            hide();
            //刷新黑名单表单
            f.bind();
            this.txtRemark.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtRemark.Text = "";
            hide();
        }
    }
}
